import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/up-form.vue"; //弹框form

import {
    saveKeyInfo, //保存
    getKeyInfo, //详情
} from "@/api/BackStage/stationProductionPage/request.js";

export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
    ],
    data() {
        return {
            visible: "",
            title: null,
            isLock: false,
            dialogHeight: null,
            formObj: formJs.formObj, //基础信息
            formData: {
                fId: "", //传fid表示修改 不传表示新增
                projectId: "", //项目id
                projectName: "", //项目名称
                companyId: "", //标段id
                companyName: "", //标段名称
                engineeringType: "", //工程类型
                engineeringName: "", //关键工程名称
                startEndNumber: "", //起讫桩号
                keyGeneralization: "", //关键工程概况
                testNum: "", //无人机监测次数
                lastCompany: "", //最后一次监测单位
                lastTestDate: "",
                lastTestDates: "",//最后一次监测时间
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                recordfiles: [], //删除文件id集合
            },
            fileList: [],
            btnShow: true,
            newRow: null,
            timeYMD: "",
            timeYmdHms: "",

        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }

            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.newRow = newVal;
                }
            },

            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
        this.getData();

    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },

    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.btnShow = true;
        this.formObj.map((item) => {
            item.disabled = false;
        });
        this.formObj[0].disabled = true;
        this.formObj[1].disabled = true;
        this.formObj[5].disabled = true;
        if (this.statusNum == 1) { //新增
            this.getDetail();
        } else if (this.statusNum == 2) { //编辑
            this.getDetail(this.newRow.fId);
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            // 闭合
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.getDetail(this.newRow.fId);
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.3 + "px";
        },
        // handleSelect(key) {
        //     this.activeIndex = key;
        // },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            // console.log("selectChange", data, a, b);
            if (b == "engineeringType") {
                this.formData.engineeringType = data.label;
            }
        },
        edit() {
            this.formData = this.$options.data.call(this).formData;
        },
        // 详情
        getDetail(id) {
            var that = this
            getKeyInfo({ fId: id }).then((res) => {
                if (res.errcode == 0) {
                    that.formData = res.data.yjQmpMsKeyengineeringinfo;
                    that.fileList = res.data.fFilelist;//文件
                    that.formData.recordfiles = [];

                    if (that.statusNum == 1) {
                        that.formData.companyId = that.userInfo.companyId;//标段id
                        that.formData.companyName = that.userInfo.companyName; //标段名称
                        that.formData.projectId = that.userInfo.projectId;//项目id
                        that.formData.projectName = that.userInfo.projectName; //项目名称
                    }
                    if (that.statusNum == 2 || that.statusNum == 1) {
                        if (that.formData.lastTestDates == "" || that.formData.lastTestDates == null) {
                            that.formData.lastTestDates = that.timeYMD;
                        }
                        if (that.formData.lastCompany == "" || that.formData.lastCompany == null) {
                            that.formData.lastCompany = that.userInfo.companyName;
                        }
                    }
                    

                }
            });
        },
        // 刪除文件
        delteFile(file) {
            var that = this;
            for (let index = 0; index < that.fileList.length; index++) {
                if (file.uid == that.fileList[index].uid) {
                    if (that.fileList[index].atta == undefined) {
                        that.formData.recordfiles.push({
                            fId: that.fileList[index].fId,
                        });

                    } else {
                        that.formData.recordfiles.push({
                            fId: that.fileList[index].atta.fId,
                        });
                    }
                    that.fileList.splice(index, 1);
                }
            }


        },
        // 保存
        yj_saveForm() {
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }

        },
        saveForm(obj) {
            this.isLock = true
            saveKeyInfo(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("保存成功");
                        this.isLock = false
                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log(error.msg);
                });
        },
        // 保存
        // 时间
        getData() {
            var time = new Date();
            const y = time.getFullYear();
            const m = (time.getMonth() + 1 + "").padStart(2, "0");
            const d = (time.getDate() + "").padStart(2, "0");
            const hh = (time.getHours() + "").padStart(2, "0");
            const mm = (time.getMinutes() + "").padStart(2, "0");
            const ss = (time.getSeconds() + "").padStart(2, "0");
            this.timeYMD = `${y}-${m}-${d}`;
            this.timeYmdHms = `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
        },
    },
};